package com.demo.java.OD551_600.OD564;

import java.util.Scanner;
import java.util.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【最长的顺子(B卷-100分)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146541172
 */
public class OdMain {
    public static String[] all_cards = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; // 所有的牌面值

    public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String current_cards = in.nextLine(); // 当前手中的牌
        String left_cards = in.nextLine();    // 已出过的牌

        // 使用哈希表记录剩余牌的数量
        HashMap<String, Integer> cards = new HashMap<>();
        for (String card : all_cards) {
            cards.put(card, 4);  // 初始时每种牌有4张
        }

        // 减去当前手中的牌和已出过的牌
        diff(cards, current_cards);
        diff(cards, left_cards);

        String result = "NO-CHAIN";  // 默认没有找到连续的牌链

        int left = 0, right = 0;
        // 遍历所有的牌，找出连续的五张牌
        for (int i = 0; i < all_cards.length; i++) {
            String card = all_cards[i];
            if (cards.get(card) > 0) {  // 如果该牌存在
                left = i;
                // 查找连续的牌
                while (i < all_cards.length - 1 && cards.get(all_cards[i + 1]) > 0) {
                    i++;
                }
                right = i + 1;  // 记录连续牌链的右边界
            }
        }

        // 如果找到连续的五张牌
        if (right - left >= 5) {
            StringBuilder builder = new StringBuilder();
            // 构建牌链并输出
            for (int i = left; i < right; i++) {
                builder.append(all_cards[i]).append("-");  // 拼接牌链
            }

            // 去掉末尾多余的"-"
            if (builder.length() > 0) {
                result = builder.substring(0, builder.length() - 1);
            }
        }

        // 输出结果
        System.out.println(result);
    }

    // 用于从字符串中减去已出的牌
    private static void diff(HashMap<String, Integer> cards, String str) {
        for (String card : str.split("-")) {
            if (cards.containsKey(card)) {
                cards.put(card, cards.get(card) - 1);  // 减少剩余的牌数量
            }
        }
    }
}